﻿using ConfExpert.Forms.Base;

using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    public partial class HataliUKMK : XForm
    {
        public HataliUKMK()
        {
            InitializeComponent();
        }

        private void HataliUKMK_Load(object sender, EventArgs e)
        {
            
            DataSet d = ExecuteDataSet("GetHataliUK");
            DataTable dt = d.Tables[0];

            DataSet d2 = ExecuteDataSet("GetHataliMK");
            DataTable dt2 = d2.Tables[0];

            gvUreticiKodu.Init(dt);
            gvUreticiKodu.AddDetailGrid(gvArtikelListe1, "UreticiKodu");

            gvArtikelListe1.DataTable().PrimaryKey = new DataColumn[] { gvArtikelListe1.DataTable().Columns["UreticikodlariId"] };
            gvUreticiKodu.AddColumn("KacArtikeldeVar", typeof(int), "COUNT(Child({FK}).UreticiKodlariId)");

            gvMusteriKodu.Init(dt2);
            gvMusteriKodu.AddDetailGrid(gvArtikelListe2, "MusteriKodu");

            gvArtikelListe2.DataTable().PrimaryKey = new DataColumn[] { gvArtikelListe2.DataTable().Columns["MusterikodlariId"] };
            gvMusteriKodu.AddColumn("KacArtikeldeVar", typeof(int), "COUNT(Child({FK}).MusteriKodlariId)");

            gvArtikelListe1.MakeColumnInvisible("UreticiKodlariId");
            gvArtikelListe2.MakeColumnInvisible("MusteriKodlariId");

            gvArtikelListe1.DisableRowDelete();
            gvArtikelListe2.DisableRowDelete();
            gvUreticiKodu.DisableRowDelete();
            gvMusteriKodu.DisableRowDelete();
            gvUreticiKodu.BestFitColumns();
            gvArtikelListe1.BestFitColumns();
            gvMusteriKodu.BestFitColumns();
            gvArtikelListe2.BestFitColumns();

            GridManager gm1 = GridManager.GetManager(gvArtikelListe1);
            GridManager gm2 = GridManager.GetManager(gvArtikelListe2);

            if (ytk(108))
            {
                gm1.GridMenu.AddMenu("Üretici kodunu bu artikelden sil", UKSil_Click);
                gm2.GridMenu.AddMenu("Müşteri kodunu bu artikelden sil", MKSil_Click);
            }
        }

        private void MKSil_Click(object sender, EventArgs e)
        {
            string mkIds = GetMKId();

            if (mkIds != String.Empty)
            {
                gvArtikelListe2.CloseEditor();
                ExecuteNonQuery("UpdMKSil", mkIds);

                foreach (int i in MKId)
                {
                    gvArtikelListe2.DataTable().Rows.Find(i).Delete();
                }
            }
        }

        private void UKSil_Click(object sender, EventArgs e)
        {
            string ukIds = GetUKId();

            if (ukIds != String.Empty)
            {
                gvArtikelListe1.CloseEditor();
                ExecuteNonQuery("UpdUKSil", ukIds);

                foreach (int i in UKId)
                {
                    gvArtikelListe1.DataTable().Rows.Find(i).Delete();
                }
                //gvArtikelListe1.RefreshData();
            }
        }

        List<int> UKId;
        private string GetUKId()
        {
            UKId = new List<int>();
            foreach (int rowHandle in gvArtikelListe1.GetSelectedRows())
                if (rowHandle > -1)
                    UKId.Add(gvArtikelListe1.GetRowCellValue(rowHandle, "UreticiKodlariId").ToInt());

            return string.Join(",", UKId.ToArray());
        }

        List<int> MKId;
        private string GetMKId()
        {
            MKId = new List<int>();
            foreach (int rowHandle in gvArtikelListe2.GetSelectedRows())
                if (rowHandle > -1)
                    MKId.Add(gvArtikelListe2.GetRowCellValue(rowHandle, "MusteriKodlariId").ToInt());

            return string.Join(",", MKId.ToArray());
        }
    }
}